<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\model\website;

use app\admin\model\SystemWebsite;
use app\common\model\TimeModel;
use think\facade\Db;
use broadcast\redis\setRedis;
use app\models\system\WebsiteDefailt;

class UserGroup extends TimeModel
{
    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->connection  = session("website");
    }

    public function getWebsite($website)
    {
        return $this;
    }

    public function getAuthorizeNodeListByAdminId ($nowRule) {
        
        //判断权限   start  
        $low_groupList = setRedis::getCache(session("website"),"default");
        if ($low_groupList) {
            $groupList = $low_groupList;
        }else {
            $groupList = WebsiteDefailt::where('website',session("website"))->find();
            if ($groupList) $groupList = $groupList->toArray();
            setRedis::setCache(session('website'),"default",$groupList,0);
        }
        $notIdList = array();
        if (isset($groupList['configure'])) {
            $group = json_decode($groupList['configure'],true);
            if ($group['whether_teacher'] != 1 && $group['whether_lecturer_ranking'] != 1) {
                $notIdList[] = 6;
            }
            if ($group['whether_curriculum'] != 1) {
                $notIdList[] = 18;
            }
            if ($group['whether_previous'] != 1) {
                $notIdList[] = 21;
            }
            if ($group['whether_individual_stocks'] != 1) {
                $notIdList[] = 14;
            }
            if ($group['whether_market_review'] != 1) {
                $notIdList[] = 20;
            }

            if ($group['whether_index_download'] != 1) {
                $notIdList[] = 22;
            }
            if ($group['whether_advanced_courseware'] != 1) {
                $notIdList[] = 13;
            }
            if ($group['whether_warehouse_reminder'] != 1) {
                $notIdList[] = 16;
            }
            if ($group['whether_closing_reminder'] != 1) {
                $notIdList[] = 17;
            }
            if (isset($group['whether_envelopes'])) {

                if ( $group['whether_envelopes']!=1) {
                    $notIdList[] = 33;
                    $notIdList[] = 34;
                }
                
            }else{
                $notIdList[] = 33;
                $notIdList[] = 34;
            }
            
            if (!isset($group['whether_luck_draw_function'])) {
                $notIdList[] = 36;
            }else{
                if ($group['whether_luck_draw_function'] != 1) {
                    $notIdList[] = 36;
                }
            }

            if (!isset($group['whether_gift_function'])) {
                $notIdList[] = 40;
            }else{
                if ($group['whether_gift_function'] != 1) {
                    $notIdList[] = 40;
                }
            }

            
        }
        //判断权限   end    

        $userRule = Db::connect(session("website"))->name("user_rule")
        ->where("pid","<>",99999999)
        ->where("id","not in",$notIdList)
        ->field("id,pid,href,title")
        ->select()->toArray();

        //设置已选
        if ($nowRule !="all") {
            $nowRule = explode(',',$nowRule);
        }else{
           $nowRule = array_column($userRule, 'id');
        }
        
        $newRule = array();

        foreach($userRule as $value) {
            if($value['pid'] == 0) {
                
                //判断是否已选
                if (in_array($value['id'],$nowRule)) {
                    $value['checked'] = true;
                }else {
                    $value['checked'] = false;
                }

                if($value['href'] == '') {
                    $value['title'] = "{$value['title']}";
                }else{
                    $value['title'] = "{$value['title']}【{$value['href']}】";
                }


                $children = [];

                foreach ($userRule as $v) {


                    if ($v['pid'] == $value['id']) {

                        //判断是否已选
                        if (in_array($v['id'],$nowRule)) {
                            $v['checked'] = true;
                        }else {
                            $v['checked'] = false;
                        }

                        if($v['href'] == '') {
                            $v['title'] = "{$v['title']}";
                        }else{
                            $v['title'] = "{$v['title']}【{$v['href']}】";
                        }

                        $children[] = $v;
                    }
                    
                }
                !empty($children) && $value['children'] = $children;
                $newRule[] = $value;
            }
        }
        return $newRule;
    }
}